const mongoose = require("../common/db");

const goods = new mongoose.Schema({
  goodsName: String, // 商品信息
  goodsInfo: String,  // 使用情况
  goodsLocation: String, // 所在学校
  goodsPrice: Number, // 价格
  goodsOrigin: Number, // 原始价格
  goodsImages: Array, // 商品图片
  goodsUser: String, // 上传用户
  goodsClassify: String, // 商品分类
  goodsTime: String, // 商品购买时间
  goodsAmount: Number, // 商品剩余数量
  goodsReturn: String, // 是否支持退货
  goodsCollect: Number, // 商品收藏数量
});

goods.statics.findAll = function (callBack) {
  this.find({}, callBack);
}

goods.statics.findByUsername = function (name, callBack) {
  this.find({ goodsUser: name }, callBack);
};

goods.statics.findByClassify = function (classify, callBack) {
  this.find({ goodsClassify: classify }, callBack);
};

goods.statics.findById = function (id, callBack) {
  this.findOne({ _id: id }, callBack);
};

goods.statics.findByGoodsname = function (name, callBack) {
  this.find({ goodsName: name}, callBack);
};

goods.statics.findBySearch = function (name, callBack) {
	this.find({ goodsName: {$regex:new RegExp(name)}}, callBack);
}

const goodsModel = mongoose.model("goods", goods);
module.exports = goodsModel;